package com.zjj.lbw.interview.array;

import java.util.Arrays;

/**
 * @author zhanglei.zjj
 * @description 数组三个数最大乘积-基于排序和线性扫描
 * @date 2023/9/1 8:40
 */
public class MaximumProduct {
    public static int sort(int[] arr) {
        // 先将数组排序
        Arrays.sort(arr);
        int n = arr.length;
        return Math.max(arr[0] * arr[1] * arr[n - 1], arr[n - 3] * arr[n - 2] * arr[n - 1]);
    }

    public static int getMin(int[] arr) {
        int MAX_1 = Integer.MIN_VALUE, MAX_2 = Integer.MIN_VALUE, MAX_3 = Integer.MIN_VALUE;
        int MIN_1 = Integer.MAX_VALUE, MIN_2 = Integer.MAX_VALUE;

        for (int i : arr) {
            if (i < MIN_1) {
                MIN_2 = MIN_1;
                MIN_1 = i;
            } else if (i < MIN_2) {
                MIN_2 = i;
            }

            if (i > MAX_1) {
                MAX_3 = MAX_2;
                MAX_2 = MAX_1;
                MAX_1 = i;
            } else if (i > MAX_2) {
                MAX_3 = MAX_2;
                MAX_2 = i;
            } else if (i > MAX_3) {
                MAX_3 = i;
            }
        }

        return Math.max(MIN_1 * MIN_2, MAX_1 * MAX_2 * MAX_3);
    }
}
